// stores/diceStore.js
import { defineStore } from 'pinia'

export const useDiceStore = defineStore('dice', {
  state: () => ({
    dices: [], // 初始状态
  }),

  getters: {
    getDices: (state) => state.dices, // 等价于 Vuex 的 getter
  },

  actions: {
    // 加载骰子数据（从本地存储）
    loadDices() {
      const dices = uni.getStorageSync('dices') || []
      this.dices = dices.map(d => ({
        ...d,
        faces: d.faces.map(f => ({ ...f })), // 深拷贝 faces
      }))
    },

    // 保存骰子数据到本地存储
    saveDices() {
      uni.setStorageSync('dices', this.dices)
    },

    // 添加骰子
    addDice(dice) {
      this.dices.push(dice)
      this.saveDices() // 直接调用其他 action
    },

    // 更新骰子
    updateDice(index, dice) {
      this.dices.splice(index, 1, dice)
      this.saveDices()
    },

    // 删除骰子
    deleteDice(index) {
      this.dices.splice(index, 1)
      this.saveDices()
    },
  },
})